package com.play.ballen.me.vm

import androidx.lifecycle.MutableLiveData
import com.play.ballen.me.entity.ClaimedInfo
import com.play.ballen.network.ApiRepository
import com.play.ballen.network.BallenRxHelper
import com.yuven.appframework.busevent.SingleLiveEvent
import com.yuven.appframework.network.RxSchedulerHelper
import com.yuven.appframework.util.addToDisposable
import com.yuven.baselib.component.viewmodel.BaseViewModel
import com.yuven.baselib.utils.Logger

open class ClaimedVM : BaseViewModel() {

    protected val model = ApiRepository()

    var page = 1
    private val pageSize = 20

    val listLog = MutableLiveData<List<ClaimedInfo>?>()
    val actionGetListFailure = SingleLiveEvent<String>()

    fun claimedLog() {
        val map = mutableMapOf<String, Any>()
        map["page"] = page
        map["pagesize"] = pageSize

        model.claimedLog(map)
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .subscribe({ data ->
                listLog.value = data
                Logger.i("ClaimedVM.claimedLog success list = $data")
            }, { throwable ->
                actionGetListFailure.value = throwable.message
                Logger.i("ClaimedVM.claimedLog error,msg = ${throwable.message}")
            }).addToDisposable(compositeDisposable)
    }

}